<?php
class Post_photo_model extends CI_Model {
    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    public function insert($record) {
        $this->db->insert('post_photo', $record);
        $res = $this->db->affected_rows();
        if ($res > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    // TODO: 选出一张合乎比例的
    public function get_one($post_id) {
        $this->db->select('
            ph.photo_id,
            ph.location,
            ph.photo_title,
            ph.origin_name,
            ph.width,
            ph.height
        ');
        $this->db->from('post_photo pp');
        $this->db->join('photo ph', 'pp.photo_id = ph.photo_id', 'left');
        $this->db->where('pp.post_id', $post_id);
        $this->db->order_by('pp.photo_like', 'desc');
        $this->db->limit(1);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->row_array();
        } else {
            return NULL;
        }
    }

    public function get($post_id) {
        $this->db->select('
            ph.photo_id,
            ph.location,
            ph.photo_title,
            ph.origin_name,
            ph.width,
            ph.height
        ');
        $this->db->from('post_photo pp');
        $this->db->join('photo ph', 'pp.photo_id = ph.photo_id', 'left');
        $this->db->where('pp.post_id', $post_id);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return NULL;
        }
    }

    public function get_all() {
        $this->db->from('post_photo');

        $this->db->join('photo', 'post_photo.photo_id = photo.photo_id');

		$this->db->join('post', 'post.post_id = post_photo.post_id');
		$this->db->order_by("post.post_time", "desc");
		$this->db->where('photo.facewall_allow', 1);

        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return NULL;
        }
    }

	public function get_one_page($num,$last_id) {
        $this->db->select('
		photo.photo_id,
		photo.location,
		photo.photo_title,
		photo.origin_name,
		photo.width,
		photo.height,
		post.post_id,
		post.post_title,
		post.user_id,
		post.post_time,
		post.post_topic,
		post_photo.photo_like,
		post_photo.post_photo_id as if_like
		');
		$this->db->join('photo', 'post_photo.photo_id = photo.photo_id');
		$this->db->join('post', 'post.post_id = post_photo.post_id');

		$this->db->where('photo.facewall_allow', 1);
		$this->db->order_by("photo.photo_id", "desc");
		$this->db->where(array('photo.photo_id <' => $last_id));

        $query = $this->db->get('post_photo',$num,0);
        if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return NULL;
        }
    }

	public function get_first_page($num) {

		$this->db->select('
		p1.photo_id,
		p1.location,
		p1.photo_title,
		p1.origin_name,
		p1.width,
		p1.height,
		p2.post_id,
		p2.post_title,
		p2.user_id,
		p2.post_time,
		p2.post_topic,
		p0.photo_like,
		p0.post_photo_id as if_like
		');

        $this->db->join('photo p1', 'p0.photo_id = p1.photo_id');
		$this->db->join('post p2', 'p2.post_id = p0.post_id');

		$this->db->where('p1.facewall_allow', 1);
		$this->db->order_by("p1.photo_id", "desc");

        $query = $this->db->get('post_photo p0', $num, 0);
	    if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return NULL;
        }
    }

	public function get_like_info($num,$ip) {
		$this->db->select('post_photo_id as id');
		$this->db->where('user_ip', $ip);
		$this->db->like('photo_like_time',date("Y-m-d"));
		$query = $this->db->get('post_photo_like', $num, 0);
		if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return NULL;
        }
	}

	public function add_like_num($data,$photo_id) {
		$query = $this->db->get_where('post_photo_like', array('post_photo_id' =>$photo_id,'user_ip' =>$data['user_ip']));

		if ($query->num_rows() > 1) return FALSE;
		if ($query->num_rows() === 1){
		$oldtime = $query->row()->photo_like_time;
		$newtime = date("Y-m-d H:i:s");
		//if (substr($newtime,0,10)== "2012-11-02")
		//if (substr($oldtime,0,10) == "2012-11-01")
		if (substr($oldtime,0,10) === substr($newtime,0,10))
			return FALSE;
		if (substr($oldtime,0,10) != substr($newtime,0,10))
			$this->db->delete('post_photo_like', array('post_photo_id' =>$photo_id,'user_ip' =>$data['user_ip']));
		}

		//if ($query->num_rows() === 0) {
        $this->db->insert('post_photo_like', $data);

		$this->db->set('photo_like', 'photo_like + 1', FALSE);
		$this->db->where(array('photo_id' => $photo_id));
		$this->db->update('post_photo');

	    $res = $this->db->affected_rows();
        if ($res > 0) {
            return TRUE;
        } else {
            return FALSE;
        }

    }
}
?>